﻿// dllmain.cpp : 此DLL inject到任务管理器中
#include "../DB394D17A785AF11/inject/HttpRequestTool.hpp"
#include "../DB394D17A785AF11/inject/fun.hpp"
#include <iphlpapi.h>
#include <codecvt>
#pragma comment(lib, "iphlpapi.lib")

#define errExitBreak(log) {log2txt(log); break;} // 输出到终端窗口，适用于while里边
#define errExit(log) {log2txt(log); DeleteAppSelf(); exit(0);} // 输出到终端窗口
std::string GetMAC(); // 获取机器码
LRESULT CALLBACK WinSunProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); // 窗口回调
extern HINSTANCE hAppInstance;
DWORD WINAPI ieuywqiotvcjsdfpdqewkdlgjafdsa_inject_Thread(LPVOID lpParameter) {
	log2txt(_c("注入成功，线程运行中......"));
	HWND hWnd_game = FindWindowA(NULL, "任务管理器");
	std::wstring strTmp = L"igurhfihaf";
	HINSTANCE hMyInstance = GetModuleHandleA("taskmgr.exe");
	WNDCLASSEX wndcls;
	wndcls.cbSize = sizeof(WNDCLASSEX);
	wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndcls.hCursor = LoadCursor(NULL, IDC_CROSS);
	wndcls.hInstance = hMyInstance;
	wndcls.lpfnWndProc = WinSunProc;
	wndcls.lpszClassName = strTmp.c_str();
	wndcls.style = CS_HREDRAW | CS_VREDRAW;
	UnregisterClass(strTmp.c_str(), hMyInstance);
	if (!RegisterClassEx(&wndcls)) {
		log2txt(_c("1.create window class error!"));
		return 0;
	}
	//HWND hWnd_new = CreateWindowEx(WS_EX_TRANSPARENT | WS_EX_LAYERED | WS_EX_TOOLWINDOW,
	HWND hWnd_new = CreateWindow(strTmp.c_str(), strTmp.c_str(),
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, NULL, hMyInstance, NULL); 
	if (hWnd_new == NULL)
	{
		log2txt(_c("2.create window error！"));
		return 0;
	}

	// 设置窗口透明和
	//SetWindowLong(hWnd_new, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED | WS_EX_TOOLWINDOW);
	//SetLayeredWindowAttributes(hWnd_new, RGB(0, 0, 0), 150, LWA_ALPHA);

	ShowWindow(hWnd_new, SW_SHOW);
	UpdateWindow(hWnd_new);
	MSG Message = { 0 };
	ZeroMemory(&Message, sizeof(Message));
	while (1)
	{
		if (PeekMessage(&Message, 0, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&Message);
			DispatchMessage(&Message);
			if (Message.message == WM_QUIT) { break; }
		}
		Sleep(0);
	}
	DestroyWindow(hWnd_new);
	Sleep(1000);
	return 0;
}

LRESULT CALLBACK WinSunProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg)
	{
		// 用WM_PAINT畫會卡窗口
		/*case WM_PAINT:
			break;*/
	case WM_CREATE:
		//DwmExtendFrameIntoClientArea(hwnd, &Margin);
		break;
	case WM_DESTROY: {PostQuitMessage(0); return 0; } break;
	case WM_SIZE:
		break;
	default:
		break;
	}
	return DefWindowProc(hwnd, uMsg, wParam, lParam);

}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved)
{
	HANDLE th = NULL;
	switch (dwReason)
	{
	case DLL_QUERY_HMODULE:
		if (lpReserved != NULL)
			*(HMODULE*)lpReserved = hAppInstance;
		break;
	case DLL_PROCESS_ATTACH:
		// 创建线程
		th = CreateThread(0, 0, ieuywqiotvcjsdfpdqewkdlgjafdsa_inject_Thread, 0, 0, 0);
		if (NULL != th) CloseHandle(th);
		break;
	case DLL_PROCESS_DETACH:
		break;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	}
	return 1;
}



// 获取机器码
std::string GetMAC() {
	// 尝试获取网络适配器信息
	IP_ADAPTER_INFO adapterInfo[16];
	DWORD bufferSize = sizeof(adapterInfo);
	DWORD result = GetAdaptersInfo(adapterInfo, &bufferSize);

	if (result == ERROR_SUCCESS) {
		// 遍历适配器信息，找到活动的适配器
		IP_ADAPTER_INFO* adapter = adapterInfo;
		while (adapter) {
			if (adapter->GatewayList.IpAddress.String[0] != '0') {
				// 构造 MAC 地址字符串
				char macAddress[18];
				sprintf_s(macAddress, _c("%02X:%02X:%02X:%02X:%02X:%02X"),
					adapter->Address[0], adapter->Address[1], adapter->Address[2],
					adapter->Address[3], adapter->Address[4], adapter->Address[5]);
				return std::string(macAddress);
			}
			adapter = adapter->Next;
		}
	}

	// 如果无法获取到 MAC 地址，获取硬盘序列号作为机器码
	DWORD volumeSerialNumber;
	if (GetVolumeInformationA(_c("C:\\"), nullptr, 0, &volumeSerialNumber, nullptr, nullptr, nullptr, 0)) {
		char machineCode[9];
		sprintf_s(machineCode, _c("%08X"), volumeSerialNumber);
		return std::string(machineCode);
	}

	// 如果无法获取到硬盘序列号，则返回空字符串
	return "";
}


std::string ConvertWCharToString(const wchar_t* wcharString)
{
	std::wstring ws(wcharString);
	std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
	return converter.to_bytes(ws);
}
std::string GetProcessNameFromPID(DWORD pid)
{
	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hSnapshot == INVALID_HANDLE_VALUE)
	{
		return "";
	}
	PROCESSENTRY32 pe32;
	pe32.dwSize = sizeof(PROCESSENTRY32);

	if (!Process32First(hSnapshot, &pe32))
	{
		CloseHandle(hSnapshot);
		return "";
	}
	do
	{
		if (pe32.th32ProcessID == pid)
		{
			CloseHandle(hSnapshot);
			return ConvertWCharToString(pe32.szExeFile);
		}
	} while (Process32Next(hSnapshot, &pe32));

	CloseHandle(hSnapshot);
	return "";
}